﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using OpenQuarters.WebQuarters.Core;
using RssToolkit;
using OpenQuarters.WebQuarters.Utils;

namespace OpenQuarters.WebQuarters.Modules.RSSReader
{
    public class RSSReaderModule : Module
    {
        public RSSReaderModule()
            : base()
        {
        }

        public static Dictionary<string, string> Templates
        {
            get
            {
                var d = new Dictionary<string, string>();
                d.Add("", "[Default]");
                foreach (var dir in new DirectoryInfo(Oracle.Context.Server.MapPath("~/Templates/")).GetDirectories())
                {
                    if (dir.Name.Contains(typeof(RSSReaderModule).FullName))
                    {
                        foreach (var template in dir.GetFiles("*.ascx", SearchOption.TopDirectoryOnly))
                        {
                            d.Add(template.Name.Replace(".ascx", ""), template.Name.Replace(".ascx", ""));
                        }
                    }
                }
                return d;
            }
        }

        private string template = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.DropDown, OptionSource = "OpenQuarters.WebQuarters.Modules.RSSReader.RSSReaderModule.Templates")]
        public string Template
        {
            get { return template; }
            set { template = value; }
        }

        private int count = 10;

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        public string feed_Url = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Feed_Url
        {
            get { return feed_Url; }
            set { feed_Url = value; }
        }

        public override void Initialise()
        {
            this.ModuleName = "RSS Reader Module";
            this.ModuleDescription = "Provides a list of feed items from an external RSS Feed";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);


            try
            {
                RssToolkit.Rss.RssDocument rss = RssToolkit.Rss.RssDocument.Load(new System.Uri(this.feed_Url));
                rss.Channel.Items = rss.Channel.Items.Take(this.count).ToList();

                if (this.Template.Length == 0)
                {
                    this.Controls.Add(new LiteralControl("No template selected"));
                }
                else
                {
                    var viewPath = "~/Templates/" + this.GetType().FullName + "/" + this.Template + ".ascx";
                    //this.Controls.AddLiteral(GeneralUtils.RenderViewToString(
                    //        Oracle.CurrentControllerContext,
                    //        viewPath,
                    //        null,
                    //        new ViewDataDictionary(rss),
                    //        new TempDataDictionary()));
                    this.Controls.AddLiteral(GeneralUtils.RenderPartialToString(viewPath, rss));
                }
            }
            catch
            {
                this.Controls.AddLiteral("The feed or selected template is invalid");
            }

        }

    }
}
